<template>
	<view class="container">
		<view class="user-info">
			<hb-cell :label="`${$t('my.head')}`" @click="onImageUpload">
				<view class="user-head" slot="right">
					<hb-head width="72" height="72" :avatar="userInfo.avatar"></hb-head>
					<image class="arrow" src="~@/static/images/arrow-right-icon.png"></image>
				</view>
			</hb-cell>
			<hb-cell :label="`${$t('my.nickname')}`" @click="setName = !setName">
				<view class="user-name" slot="right">
					<text>{{userInfo.nickname}}</text>
					<image class="arrow" src="~@/static/images/arrow-right-icon.png"></image>
				</view>
			</hb-cell>
			<hb-cell label="ID">
				<view class="user-id" slot="right">
					<text>{{userInfo.id}}</text>
				</view>
			</hb-cell>
			<hb-cell :label="`${$t('my.version')}`">
				<view class="user-id" slot="right">
					<text>{{version}}</text>
				</view>
			</hb-cell>
		</view>
		<view>
			<input type="file" value="" />
		</view>
		<button size="verylarge" class="hb-button" :loading="loading" @click="onUserLogout">{{$t('my.loginout')}}</button>
		<hb-dialog :title="`${$t('my.changeNick')}`" v-if="setName" @cancel="setName = !setName" @confirm="updateProfileEdit" :confirmText="`${$t('info.sure')}`" :cancelText="`${$t('info.cancel')}`">
			<view slot="content">
				<input type="text" v-model="newName" :placeholder="`${$t('my.changeMessage')}`" class="input">
			</view>
		</hb-dialog>
	</view>
</template>

<script>
	import base64Code from '@/plugin/base64code.js';
	import {
		userProfileAvatar,
		profileEdit,
		userLogout
	} from '@/api/user';
	import {mapState} from 'vuex'
	import fileUpload from '@/plugin/file-upload';
	import HbCell from "@/components/hb-cell";
	import HbDialog from "@/components/hb-dialog";
	import HbHead from "../../../components/hb-head";
	export default {
		name: "index",
		components: {
			HbDialog,
			HbHead,
			HbCell
		},
		data() {
			return {
				loading: false,
				name: '',
				setName: false,
				newName: '',
				version: '1.0.0',
			}
		},
		onShow() {
			
		},
		computed: {
			...mapState({
				userInfo: state => state.user.userInfo
			}),
		},
		created() {
			// #ifdef APP-PLUS
			let _this = this;
			plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
				_this.version = widgetInfo.version;
			});
			// #endif
		},
		methods: {
			onUserLogout() {
				userLogout().then(() => {
					uni.removeStorageSync('authorizationToken');
					uni.reLaunch({
						url: '/pages/login/index'
					});
				}).catch(() => {
				})
			},
			updateProfileEdit() {
				uni.showLoading();
				var time = Math.round(new Date().getTime()/1000);
				var data = {
					time:time,
					nickname:this.newName
				}
				profileEdit(data).then(() => {
					this.setName = false;
					this.$store.dispatch('getUserProfileAction');
					uni.hideLoading();
				}).catch(() => {
					uni.hideLoading();
				})
			},
			onImageUpload() {
				fileUpload('', userProfileAvatar()).then(() => {
					this.$store.dispatch('getUserProfileAction');
					uni.hideLoading();
				}).catch((error) => {
					uni.showToast({
						title: error.msg
					});
					uni.hideLoading();
				});
			},
		}
	}
</script>

<style scoped lang="scss">
	page {
		background: $hb-bg;
	}

	.container {
		background: $hb-bg;
		min-height: 100%;
		overflow: hidden;
		padding-bottom: 80rpx;
		box-sizing: border-box;

		.user-info {
			margin-top: 24rpx;
			padding: 0 32rpx;
			background: #fff;

			.user-name,
			.user-id,
			.user-head {
				display: flex;
				align-items: center;

				.arrow {
					display: block;
					width:16rpx;
					height:30rpx;
					margin-left:10rpx;
				}
			}

			.user-name,
			.user-id {
				color: $hb-text-color;
				font-size: 28rpx;
			}

			.user-id {
				padding-right: 40rpx;
			}
		}

		.input {
			font-size: 28rpx;
			color: $hb-text-color;
		}

		.hb-button {
			display: block;
			width: 100%;
			padding: 30rpx 0;
			margin-top:40rpx;
			background: #fff!important;
			font-size: 32rpx;
			border-radius: 0;
			color: #DF482F;
			border: 0!important;
			line-height:50rpx;
			&:after{
				border:0;
			}
		}
	}
</style>
